package com.spa.domain.service;

import cn.hutool.core.bean.BeanUtil;
import com.spa.domain.entity.AuthUser;
import com.spa.infrastructure.enums.dict.base.RequestSourceEnum;
import com.spa.infrastructure.enums.dict.base.SourceTypeEnum;
import com.spa.infrastructure.mapper.AuthUserMapperStruct;
import com.spa.infrastructure.repository.condition.AuthUserCondition;
import com.spa.infrastructure.repository.po.AuthUserPO;
import com.spa.infrastructure.repository.service.AuthUserRepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthUserService {

    @Resource
    private AuthUserRepositoryService authUserRepositoryService;

    public boolean addInfo(AuthUserPO po){
        authUserRepositoryService.addInfo(po);
        return true;
    }

    public AuthUser infoByOpenIdAndSource(String openId, SourceTypeEnum source, RequestSourceEnum requestSourceEnum){
        AuthUserCondition condition = new AuthUserCondition();
        condition.setOpenId(openId);
        condition.setSource(source.getCode());
        condition.setUserSource(requestSourceEnum.getCode());
        return AuthUserMapperStruct.INSTANCE.po2Entity(authUserRepositoryService.listInfoOne(condition));
    }

    public AuthUser info(Long userId, String source,String userSource){
        AuthUserCondition condition = new AuthUserCondition();
        condition.setUserId(userId);
        condition.setSource(source);
        condition.setUserSource(userSource);
        AuthUserPO po = authUserRepositoryService.listInfoOne(condition);

        if(BeanUtil.isNotEmpty(po)) return AuthUserMapperStruct.INSTANCE.po2Entity(po);
        return null;

    }

    public boolean updateInfoById(AuthUserPO authUserPo){
        return authUserRepositoryService.updateById(authUserPo);
    }

    /**
     * 通过userId清空用户第三方授权
     * @param userId
     * @return
     */
    public boolean clearAuth(Long userId){
        AuthUserCondition condition = new AuthUserCondition();
        condition.setUserId(userId);
        return authUserRepositoryService.clearAuth(condition);
    }

}
